package com.leyou.search.web;

import com.leyou.search.dto.SearchParamDTO;
import com.leyou.search.entity.Goods;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.entity.PageInfo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

/**
 * @author BaiZY on 2020/8/31.
 * @version 1.0
 */
@RestController
@RequestMapping("/goods")
public class SearchController {
    private final SearchService searchService;

    public SearchController(SearchService searchService) {
        this.searchService = searchService;
    }

    /**
     * 初始化索引库
     */
    @GetMapping("/initialization")
    public ResponseEntity<String> init(){
        searchService.createIndexAndMapping();
        searchService.loadData();
        return ResponseEntity.ok("导入成功");
    }

    /**
     * 自动补全
     * @param key
     * @return
     */
    @GetMapping("/suggestion")
    public Mono<List<String>> getSuggestion(@RequestParam("key") String key){
        return searchService.getSuggestion(key);
    }

    /**
     * 搜索框搜索
     * @param searchParamDTO
     * @return
     */
    @PostMapping("/list")
    public  Mono<PageInfo<Goods>> searchGoods (@RequestBody SearchParamDTO searchParamDTO){
        return searchService.searchGoods(searchParamDTO);
    }

    /**
     * 过滤
     */
    @PostMapping("/filter")
    public Mono<Map<String,List<?>>> listFilter(@RequestBody SearchParamDTO searchParamDTO){
        return searchService.listFilter(searchParamDTO);
    }
}
